#include <QFile>
#include <QTimer>
#include <QCloseEvent>
#include <QString>

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    initSysTrayIcon();

    int remind_time;
    int work_time;
    int relax_time;


    QFile file("init.dat");
    if(!file.open(QIODevice::ReadOnly|QIODevice::Text))//以读的方式打开文件
    {
        remind_time=30;
        work_time=25;
        relax_time=5;
    }
    else{
        QByteArray buf;
        buf = file.readLine();//读取一行的数据
        remind_time = buf.toInt();
        buf = file.readLine();//读取一行的.数据
        work_time = buf.toInt();
        buf = file.readLine();//读取一行的数据
        relax_time = buf.toInt();
    }

    file.close();//关闭文件

    // 设置能显示的位数
    ui->TCer->setDigitCount(8);
    // 设置显示的模式为十进制
    ui->TCer->setMode(QLCDNumber::Dec);

    setAutoFillBackground(true);
    // 水
    setPalette(QPalette(QColor(129,199,212)));


    ui->Tomato->setFlat(true);
    ui->Tomato->setAutoFillBackground(true);
    // 清碧
    ui->Tomato->setPalette(QPalette(QColor(38,135,133)));


    ui->SetButton->setFlat(true);
    ui->SetButton->setAutoFillBackground(true);
    // 飘
    ui->SetButton->setPalette(QPalette(QColor(0,98,132)));

    ui->Browser->setAutoFillBackground(true);
    // 瓶觐
    ui->Browser->setPalette(QPalette(QColor(165,222,228)));

    eyes = new Eyes;
    eyes->setWindowFlags(Qt::WindowStaysOnTopHint);
    eyes->setWindowTitle("提醒");

    tomato = new Tomato;
    tomato->setWindowFlags(Qt::WindowStaysOnTopHint);
    tomato->SetDefault(60*work_time,60*relax_time);
    tomato->setWindowTitle("番茄钟");


    setForm = new Set;
    setForm->SetDefault(remind_time, work_time,relax_time);
    setForm->setWindowTitle("设置");

    timer = new QTimer(this);

    connect(timer, SIGNAL(timeout()), this, SLOT(count()));

    timer->start(1000);
}

void MainWindow::count(){
    time_count.increase();
    LCDChange();

    tomato->time_count.set_time(time_count.to_uint());
    tomato->Draw();
    if(time_count.to_uint() / (circle*MAX_Time)){
        circle++;
        ShowEyesForm();
    }
}

void MainWindow::LCDChange(){
    QString str = time_count.to_str();
    ui->TCer->display(str);

};

void MainWindow::ShowEyesForm()
{
    sysTrayIcon->showMessage(tr("休息一下吧"), tr("已经工作")+QString::number(time_count.to_uint()/60)+tr("分钟了"), QSystemTrayIcon::Information, 5000);
    eyes->show();
}

void MainWindow::on_Tomato_clicked()
{   
    tomato->show();
}

void MainWindow::on_SetButton_clicked()
{
    setForm->show();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initSysTrayIcon()
{
    sysTrayIcon = new QSystemTrayIcon(this);
    sysTrayIcon->setIcon(QIcon(":Resources/ico.ico"));//设置托盘图标的icon
    sysTrayIcon->show();//展示系统托盘图片

    QMenu *menuTray = new QMenu;

    QAction *actQuit = new QAction(tr("退出"), menuTray);

    menuTray->addAction(actQuit);

    sysTrayIcon->setContextMenu(menuTray);
    sysTrayIcon->setToolTip(tr("计算机操作者健康管理"));
    sysTrayIcon->showMessage(tr("提示"), tr("Develop by RQY."), QSystemTrayIcon::Information, 1000);

    connect(sysTrayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
            this, SLOT(onSysTrayIconActived(QSystemTrayIcon::ActivationReason)));

    connect(actQuit, SIGNAL(triggered()), qApp, SLOT(quit()));
}

void MainWindow::onSysTrayIconActived(QSystemTrayIcon::ActivationReason reason)
{
    switch(reason)
    {
    case QSystemTrayIcon::Trigger://单击
    case QSystemTrayIcon::DoubleClick://双击
    {
        if (isHidden())
        {
            show();
        }
        else
        {
            setTopWindow();
        }
        break;
    }
    default:
        break;
    }
}

void MainWindow::setTopWindow()
{
    Qt::WindowStates winStatus = Qt::WindowNoState;
    if (windowState() & Qt::WindowMaximized)
    {
        winStatus = Qt::WindowMaximized;
    }
    setWindowState(Qt::WindowActive | winStatus);
    activateWindow();
    raise();
}

void MainWindow::closeEvent(QCloseEvent* event)
{
    this->hide();	 // 隐藏窗口
    event->ignore(); // 忽视原来的关闭事件
}
